import com.cos.mb3.dao.EmpDao;
import com.cos.mb3.entity.Emp;
import com.cos.mb3.util.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author: COS
 * @time: 2022/6/1 9:16
 * @description:
 */
public class Demo {
    // Cause: java.io.NotSerializableException: 对象没有实现序列化接口,无法保存起来

    /**
     *
     * @throws IOException
     * 使用二级缓存
     * 1. 2个地方需要配置 config.xml 需要配置settings 里面设置 cacheEnabled 为true
     *                   mapper.xml 需要配置 <cache/> 标签
     * 2. 必须有关闭才会把数据保存到二级缓存中
     * 3. 被缓存的数据对象,必须实现序列化接口,保证对象数据可以转为二进制数据进行保存
     *
     *
     * 三级缓存 指定自定义缓存 ehcache
     */
    @Test
    public void t5() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            SqlSession s1 = factory.openSession();
            List<Emp> all = s1.getMapper(EmpDao.class).getAll();
            s1.close();
        }
        long end = System.currentTimeMillis();
        //8496
        //3373
        //3376
        System.out.println(end-start);
    }

    @Test
    public void t4() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        SqlSession s1 = factory.openSession();
        List<Emp> all = s1.getMapper(EmpDao.class).getAll();
        System.out.println(all.size());
        s1.close();
        System.out.println("=======================");
        SqlSession s2 = factory.openSession();
        List<Emp> al2 = s2.getMapper(EmpDao.class).getAll();
        System.out.println(al2.size());
        s2.close();

    }

    @Test
    public void t3() throws IOException {
        EmpDao mapper = MyBatisUtil.getMapper(EmpDao.class);
        List<Emp> l1 = mapper.getAll();
        MyBatisUtil.close();
        System.out.println(l1);
        System.out.println("================");
        EmpDao mapper2 = MyBatisUtil.getMapper(EmpDao.class);
        List<Emp> l2 = mapper2.getAll();
        System.out.println(l2);

    }
    @Test
    public void t2() throws IOException {
        Emp e = new Emp();
        e.setEname("a");
        e.setEsex("b");
        e.setSalary(1d);
        EmpDao mapper = MyBatisUtil.getMapper(EmpDao.class);
//        System.out.println(mapper.addEmp(e));

        List<Emp> empList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            empList.add(new Emp("李"+i,"女",999.0+i,"111"+i,new Date(), 103));
        }

        System.out.println(mapper.addEmpByList(empList));
        MyBatisUtil.commit();
    }
    @Test
    public void t1() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        EmpDao mapper = session.getMapper(EmpDao.class);
        Map<String,Object> data = new HashMap<>();
        data.put("esex", "男");
        data.put("minSalary", 900);
        data.put("maxSalary", 1010);
        List<Emp> empByKey = mapper.getEmpByKey(data);
        empByKey.stream().forEach(System.out::println);
    }
}
